package com.example.test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class BaseClass {

	protected WebDriver driver;
	protected String baseUrl;
	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();

	@BeforeTest
	public void setUp() throws Exception {
	    driver = new FirefoxDriver();
	    baseUrl = "http://www.dice.com/";
	    driver.manage().window().setPosition(new Point(-2000,0));
	    //driver.manage().window().maximize();
	    //driver.manage().window().setSize(new Dimension(100,100));
	    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  }

	protected void printOutAddress() {
		System.out.println(driver.findElement(By.xpath("//div[4]/input")).getAttribute("value"));
	}

	protected void rewritingAddress(String address) {
		//rewriting address
		driver.findElement(By.xpath("//div[4]/input")).clear();
		driver.findElement(By.xpath("//div[4]/input")).sendKeys(address);
	}

	protected void editAddressSectionClick() {
		//edit address section
		driver.findElement(By.xpath("//form[5]/div/div/a")).click();
	}

	protected void signOut() {
		//logout
	    driver.findElement(By.xpath("//li/a")).click();
	}

	protected void validateProfileSavedMessage() {
		//validate
	    String cell = driver.findElement(By.xpath("//span//h3")).getText();
	    assertTrue(cell.contains("Your Profile has been successfully saved"));
	}

	protected void saveClick() {
		//save button
	    driver.findElement(By.xpath("//div[12]/span/span/span/button")).click();
	}

	protected void editResumeClick() {
		//edit resume
	    driver.findElement(By.xpath("//div[2]/a[1]")).click();
	}

	protected void manageAddResumeClick() {
		//click Manage/Add Resumes
	    driver.findElement(By.xpath("//div[5]/div/div/div[2]/a[1]")).click();
	}

	protected void signIn(String email) throws InterruptedException {
		driver.findElement(By.xpath("//li[2]/a/span")).click();
	    driver.findElement(By.xpath("//input")).clear();
	    driver.findElement(By.xpath("//input")).sendKeys(email);
	    driver.findElement(By.xpath("//div[2]/input")).clear();
	    driver.findElement(By.xpath("//div[2]/input")).sendKeys("student12");
	    driver.findElement(By.xpath("//div[4]/a")).click(); Thread.sleep(1000);
	}

	@AfterTest
	public void tearDown() throws Exception {
	    driver.quit();
	    String verificationErrorString = verificationErrors.toString();
	    if (!"".equals(verificationErrorString)) {
	      fail(verificationErrorString);
	    }
	  }

}
